<template>
  <div class="trade-container">
    <div style="margin-bottom: 10px">
      <el-button type="primary" @click="addAccount">添加账号</el-button>
    </div>
    <div>
      <el-table ref="multipleTable"
                :data="tableData.data"
                tooltip-effect="dark"
                stripe
                highlight-selection-row
                max-height="750"
                style="width: 100%">
        <el-table-column prop="account" label="用户名称" fixed="right"></el-table-column>
        <el-table-column prop="id" label="用户id" fixed="right"></el-table-column>
        <el-table-column prop="roleName" label="权限名称" fixed="right"></el-table-column>
        <el-table-column prop="state" label="状态" fixed="right">
          <template #default="scope">{{scope.row.state === '0' ? '正常' : '冻结'}}</template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-link type="success" style="margin-right: 10px" @click="lookDetail(scope.row.id)">查看</el-link>
            <el-link type="warning" style="margin-right: 10px" @click="editUserInfo(scope.row.id)">修改</el-link>
            <el-link type="danger" @click="editRoleInfo(scope.row)">权限</el-link>
            <el-link type="danger" style="margin-left: 10px" @click="delUser(scope.row.id)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 20px">
        <el-pagination background layout="prev, pager, next" :page-size="sizeNum" :total="pageTotal" @current-change="currentChange"/>
      </div>
    </div>
  </div>
  <editUser :showPopup="showPopup" @showPopupEmit="showPopupEmit" :popupType="popupType" :userId="userId"></editUser>
  <editRole :userId="userId" :roleId="roleIds" v-if="showRole" @showRoleEmit="showRoleEmit"></editRole>
</template>

<script setup>
import { ref, reactive } from "vue";
import {
  _delUser,
  _getUserList,
} from "../../http/api.js";
import {useRouter} from "vue-router";
import {ElLoading, ElMessage, ElMessageBox} from "element-plus";
import editUser from './editUser.vue'
import editRole from './editRole.vue'
const router = useRouter();
const loginId = sessionStorage.getItem('loginId');
const exhibitionId = sessionStorage.getItem('projectId');

if(!loginId||!exhibitionId) {
  ElMessage({message: '操作异常', type: 'error' });
  localStorage.clear();
  sessionStorage.clear();
  router.replace({ path: '/'})
}
const showPopup = ref(false); // 显示编辑弹框
const popupType = ref(''); // 弹框类型
const userId = ref(''); // 用户id
const roleIds = ref(''); // roleId
const tableData = reactive({ // 表格数据
  data:[]
})
let current = ref(1);
let sizeNum = ref(15);
let pageTotal = ref(0);
let showRole = ref(false); // 权限配置
// 获取用户列表
const getUserList = () => {
  const loading = ElLoading.service({lock: true, text: 'Loading', background: 'rgba(0, 0, 0, 0.7)'});
  let params = {
    current: current.value,
    size: sizeNum.value
  }
  _getUserList(params)
      .then(res => {
        console.log(res);
        loading.close();
        if(res.data.ok) {
          let records = res.data.data.records;
          pageTotal.value = res.data.data.total
          tableData.data = records;
        }
      })
      .catch(err => {
        console.log(err);
        loading.close();
      })
}
getUserList();

// 添加账号
const addAccount = () => {
  popupType.value = '添加';
  userId.value = '';
  showPopup.value = true;
}
// 编辑点击
const editUserInfo = (id) => {
  popupType.value = '编辑';
  userId.value = id;
  showPopup.value = true;
}
// 查看详情
const lookDetail = (id) => {
  popupType.value = '详情';
  userId.value = id;
  showPopup.value = true;
}
// 监听编辑弹框显示
const showPopupEmit = (boolean) => {
  showPopup.value = boolean;
  popupType.value = '';
  userId.value = '';
  getUserList();
}
// 分页点击
const currentChange = (index) => {
  current.value = index
  getUserList();
}
// 删除用户
const delUser = (id) => {
  ElMessageBox.confirm('此操作不可逆,确定删除该用户?', '提示', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning',})
      .then(() => {
        const loading = ElLoading.service({lock: true, text: 'Loading', background: 'rgba(0, 0, 0, 0.7)'});

        _delUser(id)
            .then(res => {
              loading.close();
              if(res.data.ok) {
                ElMessage({type: 'success', message: '删除成功'})
                getUserList();
              }
            })
            .catch(err => {
              console.log(err);
              loading.close();
            })
      })
      .catch(() => {})
}
// 权限点击
const editRoleInfo = ({id,roleId}) => {
  userId.value = id;
  roleIds.value = roleId;
  showRole.value = true;
}
// 监听权限弹框事件
const showRoleEmit = () => {
  userId.value = '';
  roleIds.value = '';
  showRole.value = false;
}
</script>

<style lang="scss" scoped>
.trade-container{
  padding: 10px;
}
.icon-style{
  font-size: 24px;
  cursor: pointer;
}
.face-img-box{
  width: 50px;
  height: 65px;
}
</style>
